package com.songcha.library_common.ui.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.songcha.library_common.R
import com.songcha.library_common.util.FontUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px

/*import com.songcha.library_common.util.BitmapUtil
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px*/

class RadarMapView
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : View(context,attrs,defStyle,defRes) {

    companion object{
        //const val RADAR_POINT_COUNT = 8
        const val RADAR_WEB_COUNT = 5
    }

    private var mRadius = 0f
    private lateinit var mRadarPaint: Paint
    private var mRadarLineColor = Color.BLUE
    private var mRadarLineWidth = 2f.dp2px()
    //var mAngle = 0f
    private var mCenterX = 0f
    private var mCenterY = 0f
    private var mTitles = listOf<String>()
    private var mValues = listOf<Float>()
    private lateinit var mTextPaint: Paint
    private var mTitleColor = Color.BLUE
    private var mTitleTextSize = 12f
    private var mTitleMargin = 1f.dp2px()
    private var mValueBgColor = Color.parseColor("#7f0000ff")
    private var mValuePointColor = Color.BLUE
    private lateinit var mValuePaint: Paint
    private var mPointRadius = 3f.dp2px()
    //var mAnimValue = 0f
    private var mIsDrawTitle = true
    private var mIsDrawLine = true
    private var mIsDrawValueBackground = true
    private var mRadarPointCount = 8

    init {
        initView(context, attrs)
    }

    constructor(context: Context,titles:List<String>,
                isDrawTitle:Boolean, isDrawLine:Boolean, isDrawValueBackground:Boolean,
                webColor:Int,valueColor:Int,valueBgColor:Int,titleColor:Int,
                titleMargin:Float, radarLineWidth:Float,titleTextSize:Float = 12f) : this(context) {
        mTitles = titles
        mIsDrawTitle = isDrawTitle
        mIsDrawLine = isDrawLine
        mIsDrawValueBackground = isDrawValueBackground
        mRadarLineColor = webColor
        mValuePointColor = valueColor
        mValueBgColor = valueBgColor
        mTitleColor = titleColor
        mTitleMargin = titleMargin
        mRadarLineWidth = radarLineWidth
        mRadarPointCount = mTitles.size
        mTitleTextSize = titleTextSize

        initPaint()
    }

    fun setValues(values:List<Float>){
        mValues = values
        invalidate()
    }

    private fun initView(context: Context, attrs: AttributeSet?){

        if(attrs != null){
            val ta = context.obtainStyledAttributes(attrs, R.styleable.RadarMapView)
            mIsDrawLine = ta.getBoolean(R.styleable.RadarMapView_radarMap_isDrawLine,true)
            mIsDrawValueBackground = ta.getBoolean(R.styleable.RadarMapView_radarMap_isDrawValueBackground,true)
            mRadarLineColor = ta.getColor(R.styleable.RadarMapView_radarMap_webColor,Color.BLUE)
            mValuePointColor = ta.getColor(R.styleable.RadarMapView_radarMap_valueColor,Color.BLUE)
            mTitleColor = ta.getColor(R.styleable.RadarMapView_radarMap_titleColor,Color.BLUE)
            mTitleMargin = ta.getDimension(R.styleable.RadarMapView_radarMap_titleMargin,2f.dp2px())
            mRadarLineWidth = ta.getDimension(R.styleable.RadarMapView_radarMap_radarLineWidth,2f.dp2px())

            ta.recycle()
        }

        initPaint()
    }

    private fun initPaint(){
        mRadarPaint = Paint()
        mRadarPaint.isAntiAlias = true
        mRadarPaint.style = Paint.Style.STROKE
        mRadarPaint.strokeWidth = mRadarLineWidth
        mRadarPaint.color = mRadarLineColor

        mTextPaint = Paint()
        mTextPaint.isAntiAlias = true
        mTextPaint.color = mTitleColor
        mTextPaint.textSize = mTitleTextSize.sp2px()

        mValuePaint = Paint()
        mValuePaint.isAntiAlias = true
        mValuePaint.color = mValueBgColor
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mRadius = (Math.min(width,height) / 2) * 0.8f
        mCenterX = width / 2f
        mCenterY = height / 2f
        //mAngle = (Math.PI * 2 / RADAR_POINT_COUNT).toFloat()

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawPolygon(canvas)
        drawLine(canvas)
        drawTitle(canvas)
        drawValues(canvas)
    }

    private fun drawPolygon(canvas: Canvas){
        val path = Path()
        val radius = mRadius / RADAR_WEB_COUNT
        for(i in 0 until RADAR_WEB_COUNT){
            //if(i == 0) continue
            path.reset()
            var curRadius = radius * (i + 1)
            for(j in 0 until mRadarPointCount){
                if(j == 0){
                    path.moveTo(mCenterX + curRadius,mCenterY)
                }else {
                    val angle = (Math.PI * 2 / mRadarPointCount).toFloat() * j
                    val x = mCenterX + curRadius * Math.cos(angle.toDouble()).toFloat()
                    val y = mCenterY + curRadius * Math.sin(angle.toDouble()).toFloat()
                    path.lineTo(x,y)
                }
            }
            path.close()
            canvas.drawPath(path,mRadarPaint)
        }
    }

    private fun drawLine(canvas: Canvas){
        if(!mIsDrawLine) return
        for(i in 0 until mRadarPointCount){
            val angle = (Math.PI * 2 / mRadarPointCount).toFloat() * i
            val x = mCenterX + mRadius * Math.cos(angle.toDouble()).toFloat()
            val y = mCenterY + mRadius * Math.sin(angle.toDouble()).toFloat()
            canvas.drawLine(mCenterX,mCenterY,x,y,mRadarPaint)
        }
    }

    private fun drawTitle(canvas: Canvas){
        //TODO
        if(mTitles.isEmpty() || !mIsDrawTitle) return
        for(i in 0 until mRadarPointCount){
            val title = mTitles[i]
            val angle = (Math.PI * 2 / mRadarPointCount).toFloat() * i
            val x = mCenterX + mRadius * Math.cos(angle.toDouble()).toFloat()
            val y = mCenterY + mRadius * Math.sin(angle.toDouble()).toFloat()
            val fm = FontUtil.getFontMetrics(mTextPaint)
            val baseline = FontUtil.getCenterToBaseLine(fm)
            val textWidth = FontUtil.measureText(title,mTextPaint)
            /*when(i){
                0 -> {
                    canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                }
                1 -> {
                    canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                }
                2 -> {
                    canvas.drawText(title,x - textWidth / 2,y + mTitleMargin + baseline * 2 ,mTextPaint)
                }
                3 -> {
                    canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                }
                4 -> {
                    canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                }
                5 -> {
                    canvas.drawText(title,x - textWidth - mTitleMargin,y ,mTextPaint)
                }
                6 -> {
                    canvas.drawText(title,x - textWidth / 2 ,y - mTitleMargin ,mTextPaint)
                }
                7 -> {
                    canvas.drawText(title,x + mTitleMargin,y ,mTextPaint)
                }
            }*/

            if(mRadarPointCount == 8){
                when(i){
                    0,4->{
                        if(i == 0){
                            canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                        }
                    }
                    1,5->{
                        if(i == 1){
                            canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth - mTitleMargin,y,mTextPaint)
                        }
                    }
                    2,6->{
                        if(i == 2){
                            canvas.drawText(title,x - textWidth / 2,y + mTitleMargin + baseline * 2 ,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth / 2 ,y - mTitleMargin ,mTextPaint)
                        }
                    }
                    3,7->{
                        if(i == 3){
                            canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x + mTitleMargin,y ,mTextPaint)
                        }
                    }
                }
            }else if(mRadarPointCount == 10){
                when(i){
                    0,5->{
                        if(i == 0){
                            canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                        }
                    }
                    1,6->{
                        if(i == 1){
                            canvas.drawText(title,x + mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth - mTitleMargin,y,mTextPaint)
                        }
                    }
                    2,7->{
                        if(i == 2){
                            canvas.drawText(title,x - textWidth / 2 ,y + baseline * 2 + mTitleMargin ,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth / 2 ,y - mTitleMargin ,mTextPaint)
                        }
                    }
                    3,8->{
                        if(i == 3){
                            canvas.drawText(title,x - textWidth / 2 ,y + baseline * 2 + mTitleMargin ,mTextPaint)
                        }else{
                            canvas.drawText(title,x - textWidth / 2 ,y - mTitleMargin ,mTextPaint)
                        }
                    }
                    4,9->{
                        if(i == 4){
                            canvas.drawText(title,x - textWidth - mTitleMargin,y + baseline,mTextPaint)
                        }else{
                            canvas.drawText(title,x + mTitleMargin,y ,mTextPaint)
                        }
                    }
                }
            }

        }
    }

    private fun drawValues(canvas: Canvas){
        if(mValues.isEmpty()) return
        val path = Path()
        path.moveTo(mCenterX,mCenterY)
        for(i in 0 until mRadarPointCount){
            val angle = (Math.PI * 2 / mRadarPointCount).toFloat() * i
            val percent = mValues[i]
            val x = mCenterX + mRadius * percent * Math.cos(angle.toDouble()).toFloat()
            val y = mCenterY + mRadius * percent * Math.sin(angle.toDouble()).toFloat()
            if(i == 0){
                path.moveTo(x,y)
            }else{
                path.lineTo(x,y)
            }
            mValuePaint.color = mValuePointColor
            mValuePaint.style = Paint.Style.FILL
            canvas.drawCircle(x,y,mPointRadius,mValuePaint)
        }
        path.close()
        mValuePaint.color = mValueBgColor
        mValuePaint.strokeWidth = mRadarLineWidth
        if(mIsDrawValueBackground){
            mValuePaint.style = Paint.Style.FILL_AND_STROKE
        }else{
            mValuePaint.style = Paint.Style.STROKE
        }
        canvas.drawPath(path,mValuePaint)
    }

    /*private fun startAnim(){
        val anim = ValueAnimator.ofFloat(0f,1f)
        anim.apply {
            anim.duration = duration
            interpolator = LinearInterpolator()
            addUpdateListener {
                mAnimValue = it.animatedValue as Float
                invalidate()
            }
            start()
        }
    }*/

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        //startAnim()
    }

}